package com.benefm.multipar.pad.dashboard;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.benefm.multipar.pad.AppConfig;
import com.benefm.multipar.pad.R;
import com.benefm.multipar.pad.databinding.ActivityOnlineDeviceBinding;
import com.benefm.multipar.pad.http.HttpRequest;
import com.benefm.multipar.pad.http.HttpResult;
import com.benefm.multipar.pad.model.HomeResult;
import com.benefm.multipar.pad.model.PersonInfo;
import com.benefm.multipar.pad.ui.LoginActivity;
import com.benefm.multipar.pad.util.ACache;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OnlineDeviceActivity extends AppCompatActivity {

    private ActivityOnlineDeviceBinding binding;

    private List<PersonInfo> personList = new ArrayList<>();

    private OnlineDeviceAdapter deviceAdapter;

    private int pageIndex = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_online_device);

//        binding.toolBar.setTitle("设备列表");
        binding.recyclerView.setLayoutManager(new GridLayoutManager(this, 4));

        deviceAdapter = new OnlineDeviceAdapter(personList);
        binding.recyclerView.setAdapter(deviceAdapter);

        getOnlineDevice();

        addListener();

    }

    private void addListener() {

        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            pageIndex = 1;
            getOnlineDevice();
        });

        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            pageIndex++;
            getOnlineDevice();
        });

        deviceAdapter.setOnItemClickListener((adapter, view, position) -> {
            PersonInfo info = (PersonInfo) adapter.getItem(position);
            Intent intent = new Intent(this, DashboardActivity.class);
            intent.putExtra("info", info);
            startActivity(intent);
        });
    }


    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setMessage("此操作将会退出当前界面，请确认！")
                .setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                })
                .setPositiveButton("确定", (dialog, which) -> {
                    dialog.dismiss();
                    ACache.get(this).remove(AppConfig.LOGIN_INFO);
                    startActivity(new Intent(this, LoginActivity.class));
                    OnlineDeviceActivity.this.finish();
                })
                .create()
                .show();
    }

    private void getOnlineDevice() {

        HttpRequest.getDevices(pageIndex, 20, new Callback<HttpResult<HomeResult>>() {
            @Override
            public void onResponse(Call<HttpResult<HomeResult>> call, Response<HttpResult<HomeResult>> response) {
                if (response.body() != null && response.isSuccessful()) {
                    HomeResult homeResult = response.body().resultData;
                    int total = homeResult.total;
                    List<PersonInfo> list = homeResult.datas;
                    if (pageIndex == 1) {
                        personList.clear();
                        binding.refreshLayout.finishRefresh();
                    } else {
                        binding.refreshLayout.finishLoadMore();
                    }
                    personList.addAll(list);
                    deviceAdapter.notifyDataSetChanged();
                    binding.refreshLayout.setEnableLoadMore(personList.size() < total);

                }
            }

            @Override
            public void onFailure(Call<HttpResult<HomeResult>> call, Throwable t) {
                if (pageIndex == 1) {
                    binding.refreshLayout.finishRefresh();
                } else {
                    binding.refreshLayout.finishLoadMore();
                }
            }
        });

    }

}
